package com.zzx.config;

/**
 * @Author: zzx
 * @Date: 2025/5/24 22:51
 */
import com.zzx.common.core.text.Convert;

import java.util.Map;

public class UserContext {
    private static final ThreadLocal<Map<String, Object>> currentUser = new ThreadLocal<>();

    public static void setCurrentUser(Map<String, Object> claims) {
        currentUser.set(claims);
    }

    public static Map<String, Object> getCurrentUser() {
        return currentUser.get();
    }

    public static String getOpenId() {
        Map<String, Object> claims = currentUser.get();
        return claims != null ? (String) claims.get("openId") : null;
    }

    public static Long getUserId() {
        Map<String, Object> claims = currentUser.get();
        return claims != null ? Convert.toLong(claims.get("userId"))  : null;
    }

    public static Long getCompanyId() {
        Map<String, Object> claims = currentUser.get();
        return claims != null ? Convert.toLong(claims.get("companyId")) : null;
    }

    public static String getSessionKey() {
        Map<String, Object> claims = currentUser.get();
        return claims != null ? (String) claims.get("sessionKey") : null;
    }

    public static void clear() {
        currentUser.remove();
    }
}

